package org.darcy.controller.org;

import java.util.ArrayList;
import java.util.List;

import org.darcy.controller.BaseController;
import org.darcy.entity.org.Staff;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.org.StaffService;
import org.darcy.utils.common.ReturnCode;
import org.darcy.utils.vo.ReturnVo;
import org.darcy.vo.CheckExistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "StaffController接口：员工信息")
@RestController
@RequestMapping("/org/staff")
@Slf4j
public class StaffController extends BaseController<Staff> {

	private final String info_prefix = "[员工信息模块]";

	@Autowired
	private StaffService service;

	@Operation(summary = "员工分页列表")
	@PostMapping("list")
	public R list(@RequestBody PageParam<Staff> param) {
		log.debug("{}-列表数据", info_prefix);
		PageData<Staff> data = service.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "员工详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看员工信息", info_prefix);
		Staff data = service.getDetail(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增员工")
	@PostMapping("add")
	public R add(@RequestBody Staff record) {
		log.debug("{}-新增员工: {}", info_prefix, record);
		service.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除员工")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		service.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "更新员工")
	@PostMapping("update")
	public R update(@RequestBody Staff record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		service.update(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看员工属性")
	@GetMapping("/model")
	public Staff model() {
		return null;
	}

	/**
	 * 判断员工是否存在
	 *
	 * @param checkExistVo 参数
	 * @return
	 */
	@PostMapping(value = "/checkEntityExist", produces = "application/json")
	protected R checkEntityExist(@RequestBody CheckExistVo checkExistVo) {
		return this.checkExist(service, checkExistVo);
	}

	@Operation(summary = "批量删除员工")
	@PostMapping(value = "/delete", produces = "application/json")
	public ReturnVo<String> delete(@RequestBody List<String> ids) {
		ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK");
		service.deleteByIds(ids);
		return returnVo;
	}

	/**
	 * 通过工号查询-多个
	 *
	 * @param codes
	 * @return
	 */
	@PostMapping(value = "/getByCodes", produces = "application/json")
	public ReturnVo<List<Staff>> getByCodes(@RequestBody List<String> codes) {
		ReturnVo<List<Staff>> returnVo = new ReturnVo<>(ReturnCode.FAIL, "查询异常！");
		List<Staff> personals = new ArrayList<>();
		try {
			personals = service.getListByCodes(codes);
			returnVo.setCode(ReturnCode.SUCCESS);
			returnVo.setMsg("查询数据成功！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("通过工号查询数据异常！" + e);
		}
		returnVo.setData(personals);
		return returnVo;
	}

}
